var MJModel           = require('./MJModel');
var MJProto           = require('./MJProto');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {
		guoButton: cc.Node,
		pengButton: cc.Node,
		gangButton: cc.Node,
		huButton: cc.Node,
	},

	onLoad: function () {
		this.node.active = false;
		Global.MessageCallback.addListener('RoomMessagePush', this);
		Global.MessageCallback.addListener('GameMessagePush', this);
		Global.MessageCallback.addListener('ReConnectSuccess', this);
	},

	gameInit: function () {
		let operateArray = MJModel.getOperateArrayByChairID(MJModel.getMyChairID());
		if (operateArray == null || MJModel.getGameStatus() != MJProto.gameStatus.PLAYING) {
			this.node.active = false;
		}
		else {
			if (operateArray.length == 0 || (operateArray.length == 1 && operateArray[0] == MJProto.operateType.QI)) {
				this.node.active = false;
			}
			else {
				this.node.active = true;
				this.guoButton.active = true;
				this.pengButton.active = (operateArray.indexOf(MJProto.operateType.PENG) != -1);
				this.gangButton.active = (operateArray.indexOf(MJProto.operateType.GANG_CHI) != -1) || (operateArray.indexOf(MJProto.operateType.GANG_BU) != -1) || (operateArray.indexOf(MJProto.operateType.GANG_ZHI) != -1);
				this.huButton.active = (operateArray.indexOf(MJProto.operateType.HU_CHI) != -1) || (operateArray.indexOf(MJProto.operateType.HU_ZHI) != -1);
			}
		}
	},

	messageCallbackHandler: function(router, msg) {
		if(!MJModel.getGameInited()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				this.gameInit();
			}
		}
		else if(router === 'GameMessagePush') {
			if(msg.type === MJProto.GAME_TURN_PUSH) {
				this.gameInit();
			}
			else if(msg.type === MJProto.GAME_TURN_OPERATE_PUSH) {
				this.node.active = false;
			}
		} 
	},

	onButtonClick: function (event, param) {
		// let mainMj = this.node.parent.getComponent('MJMainDialog');
		// let myCardsLogic = mainMj.getMyCardLogic();
		let cardLogicArr = [null, null, null, null];
		let myCardsLogic = null;
		for (let i = 1; i <= 4; ++i) {
			let node = cc.find('Cards'+i, this.node.parent);
			if (node && node.active) {
				let logic = node.getComponent('MJCards');
				if (logic.getChairID() == MJModel.getMyChairID()) {
					myCardsLogic = logic;
				}
			}
		}
		let operateArray = MJModel.getOperateArrayByChairID(MJModel.getMyChairID());
		if (param == 'guo') {
			if (operateArray.indexOf(MJProto.operateType.HU_ZHI) != -1) {
				Global.DialogManager.addPopDialog("能够胡牌，确定要过吗？", () => {
					Global.NetworkManager.notify(GameMessageRouter, MJProto.gameOperateNotify(null, MJProto.operateType.GUO));
				}, () => {
					this.node.active = true;
				});
			}
			else {
				Global.NetworkManager.notify(GameMessageRouter, MJProto.gameOperateNotify(null, MJProto.operateType.GUO));
			}
		}
		else if (param == 'peng') {
			Global.NetworkManager.notify(GameMessageRouter, MJProto.gameOperateNotify(null, MJProto.operateType.PENG));
			if (myCardsLogic) {
				myCardsLogic.localPreOperate(MJProto.operateType.PENG, null);
			}
		}
		else if (param == 'gang') {
			let gangOperate = null;
			gangOperate = (operateArray.indexOf(MJProto.operateType.GANG_CHI) != -1)? MJProto.operateType.GANG_CHI:gangOperate;
			gangOperate = (operateArray.indexOf(MJProto.operateType.GANG_BU) != -1)? MJProto.operateType.GANG_BU:gangOperate;
			gangOperate = (operateArray.indexOf(MJProto.operateType.GANG_ZHI) != -1)? MJProto.operateType.GANG_ZHI:gangOperate;
			if (gangOperate != null) {
				Global.NetworkManager.notify(GameMessageRouter, MJProto.gameOperateNotify(null, gangOperate));
				if (myCardsLogic) {
					myCardsLogic.localPreOperate(gangOperate, null);
				}
			}
		}
		else if (param == 'hu') {
			let huOperate = null;
			huOperate = (operateArray.indexOf(MJProto.operateType.HU_CHI) != -1)? MJProto.operateType.HU_CHI:huOperate;
			huOperate = (operateArray.indexOf(MJProto.operateType.HU_ZHI) != -1)? MJProto.operateType.HU_ZHI:huOperate;
			if (huOperate != null) {
				Global.NetworkManager.notify(GameMessageRouter, MJProto.gameOperateNotify(null, huOperate));
				if (myCardsLogic) {
					myCardsLogic.localPreOperate(huOperate, null);
				}
			}
		}
		this.node.active = false;
	},

	onDestroy: function () {
		Global.MessageCallback.removeListener('RoomMessagePush', this);
		Global.MessageCallback.removeListener('GameMessagePush', this);
		Global.MessageCallback.removeListener('ReConnectSuccess', this);
	},
});
